package com.liangzhen.verynews;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.liangzhen.verynews.activity.Channel_list_activity;
import com.liangzhen.verynews.adapter.MainFragmentPagerAdapter;
import com.liangzhen.verynews.fragment.MainFragment;
import com.liangzhen.verynews.utils.ConnectityUtils;

public class MainActivity extends FragmentActivity implements OnClickListener {

	private ViewPager viewPager;
	private FragmentManager fragmentManager;
	private RadioButton lvyou, jiankang, shishang, youxi, junshi, hulianwang,
			tuijian, fangchan, redian;
	private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
	private Button channel;
	private SlidingMenu slidingMenu;
	private ImageButton openSliding;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		if (!ConnectityUtils.isConection(getApplication())) {
			Toast.makeText(MainActivity.this,"网络连接异常，请检查网络后重试！",Toast.LENGTH_SHORT).show();
		}
		init();
		MSlidingMenu();
		ViewpagerController();
		MainFragmentPagerAdapter mainFragmentPagerAdapter = new MainFragmentPagerAdapter(
				fragmentManager, fragments);
		viewPager.setAdapter(mainFragmentPagerAdapter);
		mainFragmentPagerAdapter.notifyDataSetChanged();
	}

	private void MSlidingMenu() {
		slidingMenu = new SlidingMenu(this);
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setMenu(R.layout.slidingmenulayout);
		int sw = getResources().getDisplayMetrics().widthPixels;
		slidingMenu.setBehindWidth(sw * 4 / 5);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
		slidingMenu.setFadeEnabled(true);
		slidingMenu.setFadeDegree(0.5f);
		openSliding.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!slidingMenu.isMenuShowing()) {
					slidingMenu.showMenu();
				} else {
					slidingMenu.toggle();
				}
			}
		});
	}

	private void ViewpagerController() {
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					lvyou.setChecked(true);
					break;
				case 1:
					jiankang.setChecked(true);
					break;
				case 2:
					shishang.setChecked(true);
					break;
				case 3:
					youxi.setChecked(true);
					break;
				case 4:
					junshi.setChecked(true);
					break;
				case 5:
					hulianwang.setChecked(true);
					break;
				case 6:
					tuijian.setChecked(true);
					break;
				case 7:
					fangchan.setChecked(true);
					break;
				case 8:
					redian.setChecked(true);
					break;
				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	void init() {
		viewPager = (ViewPager) findViewById(R.id.verynews_main_viewpager);
		lvyou = (RadioButton) findViewById(R.id.verynews_lvyou);
		jiankang = (RadioButton) findViewById(R.id.verynews_jiankang);
		shishang = (RadioButton) findViewById(R.id.verynews_shishang);
		youxi = (RadioButton) findViewById(R.id.verynews_youxi);
		junshi = (RadioButton) findViewById(R.id.verynews_junshi);
		hulianwang = (RadioButton) findViewById(R.id.verynews_hulianwang);
		tuijian = (RadioButton) findViewById(R.id.verynews_tuijian);
		fangchan = (RadioButton) findViewById(R.id.verynews_fangchan);
		redian = (RadioButton) findViewById(R.id.verynews_redian);
		channel = (Button) findViewById(R.id.channel_manager);
		openSliding = (ImageButton) findViewById(R.id.verynews_login_head);
		lvyou.setOnClickListener(this);
		jiankang.setOnClickListener(this);
		shishang.setOnClickListener(this);
		youxi.setOnClickListener(this);
		junshi.setOnClickListener(this);
		hulianwang.setOnClickListener(this);
		tuijian.setOnClickListener(this);
		fangchan.setOnClickListener(this);
		redian.setOnClickListener(this);
		channel.setOnClickListener(this);
		fragmentManager = getSupportFragmentManager();
		MainFragment lvyouFragment = new MainFragment(0);
		MainFragment jiankangFragment = new MainFragment(1);
		MainFragment shishangFragment = new MainFragment(2);
		MainFragment youxiFragment = new MainFragment(3);
		MainFragment junshiFragment = new MainFragment(4);
		MainFragment hulianwangFragment = new MainFragment(5);
		MainFragment tuijianFragment = new MainFragment(6);
		MainFragment fangchanFragment = new MainFragment(7);
		MainFragment redianFragment = new MainFragment(8);
		fragments.add(lvyouFragment);
		fragments.add(jiankangFragment);
		fragments.add(shishangFragment);
		fragments.add(youxiFragment);
		fragments.add(junshiFragment);
		fragments.add(hulianwangFragment);
		fragments.add(tuijianFragment);
		fragments.add(fangchanFragment);
		fragments.add(redianFragment);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.verynews_lvyou:
			viewPager.setCurrentItem(0);
			break;
		case R.id.verynews_jiankang:
			viewPager.setCurrentItem(1);
			break;
		case R.id.verynews_shishang:
			viewPager.setCurrentItem(2);
			break;
		case R.id.verynews_youxi:
			viewPager.setCurrentItem(3);
			break;
		case R.id.verynews_junshi:
			viewPager.setCurrentItem(4);
			break;
		case R.id.verynews_hulianwang:
			viewPager.setCurrentItem(5);
			break;
		case R.id.verynews_tuijian:
			viewPager.setCurrentItem(6);
			break;
		case R.id.verynews_fangchan:
			viewPager.setCurrentItem(7);
			break;
		case R.id.verynews_redian:
			viewPager.setCurrentItem(8);
			break;
		case R.id.channel_manager:
			Intent intent = new Intent(MainActivity.this,
					Channel_list_activity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

}
